package www.cskaoyan.com.dao;

import www.cskaoyan.com.bean.Academy;
import www.cskaoyan.com.dao.JDBCUtils.ConnectionPool;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class AcademyManagerDao {

    Connection connection;

    public int create(String academyName) throws SQLException {

        connection = ConnectionPool.getConnection();

        String sql = "insert into `academy` set  academyName = ? ";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, academyName);
        preparedStatement.executeUpdate();

        connection.close();
        return 1;
    }

    // 获得学院列表
    public List<Academy> getAcademyList() throws SQLException {
        connection = ConnectionPool.getConnection();

        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery("select * from   `academy`; ");


        List<Academy> list = new ArrayList<>();
        while (resultSet.next()){

            int id = resultSet.getInt(1);
            String academyName = resultSet.getString(2);


            Academy academy = new Academy(id, academyName);
            list.add(academy);
        }

        return list;
    }


    public void deleteAcademy(int id) throws SQLException {
        connection = ConnectionPool.getConnection();

        String sql = "delete from `academy` where id  = ? ";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, id);
        preparedStatement.executeUpdate();


        connection.close();
    }
}
